<?php
require_once '../classes/includeAll.php';
$title="Service Konfiguration";
$head='<script src="../javascript/pagesJavascript/config.js" type="text/javascript" ></script>'.
      '<link rel="stylesheet" type="text/css" href="../design/css/config.css">';


if(!$isAdmin)
{
  header("Location: index.php");
  exit();
}

require '../resources/getConnection.php';
$configClass = new Config($connection);
if(isset($_POST["submit"]))
{
  //echo print_r($_POST);
  $somethingChanged = false;
  $dhcpNet = null;
  $scanTime = filter_input(INPUT_POST, "scanTime");
  $timeout = filter_input(INPUT_POST, "timeout", FILTER_VALIDATE_INT);
  $maxThreads = filter_input(INPUT_POST, "maxThreads", FILTER_VALIDATE_INT);
  $dhcpServer = filter_input(INPUT_POST, "server", FILTER_VALIDATE_IP);
  $scpPath = filter_input(INPUT_POST, "scp");
  $scpPath = trim(str_replace('\\', '/', $scpPath),"\"");
  $debug = filter_input(INPUT_POST, "Debug");
  $enableNetwork = filter_input(INPUT_POST, "ennetwork");
  $network = filter_input(INPUT_POST, "network");
  $networkUser = filter_input(INPUT_POST, "networkUser");
  $networkUser = trim(str_replace('\\', '/', $networkUser),"\"");
  $networkPas = filter_input(INPUT_POST, "networkPassword");
  if(isset($_POST["dhcpnet"]))
    $dhcpNet = $_POST["dhcpnet"];
  if($scanTime != null && $scanTime != false)
  {
    if(strpos($scanTime, ":")!==false)
    {
      $timecheck= spliti(":", $scanTime);
      $number = count($timecheck);
      if($number ==2 || $number == 3)
      {
        $valid = true;
        for($i = 0; $i<$number;$i++) {
          if(is_numeric($timecheck[$i]))
          {
            if($i == 0 && ($timecheck[$i]>=24 || $timecheck[$i]<0))
              $valid = false;
            else if($timecheck[$i]>=60 || $timecheck[$i]<0)
              $valid = false;
          }
          else
            $valid = FALSE;
        }
        if($valid)
        {
          $somethingChanged = true;
          $configClass->EntryExistsOrCreate ("ScanTime",$scanTime);
        }
      }
    }
  }
  if($timeout != null && $timeout != false)
  {
    $somethingChanged = true;
    $configClass->EntryExistsOrCreate ("Timeout",$timeout);
  }
  if($maxThreads != null && $maxThreads != false)
  {
    $somethingChanged = true;
    $configClass->EntryExistsOrCreate ("MaxThreads",$maxThreads);
  }
  if($dhcpServer != null && $dhcpServer != false)
  {
    $somethingChanged = true;
    $configClass->EntryExistsOrCreate ("DHCPAddress",$dhcpServer);
  }
  if($scpPath != null && $scpPath != false)
  {
    $configClass->EntryExistsOrCreate ("SCPPath",$scpPath);
    
  }
  if($debug != null && $debug != false)
  {
    $oldDebug = $configClass->GetConfigEntryByName("Debug");
    if(!$oldDebug || $oldDebug != $debug)
    {
      $somethingChanged = true;
      $configClass->EntryExistsOrCreate ("Debug", $debug);
    }
  }
  else
  {
    $oldDebug = $configClass->GetConfigEntryByName("Debug");
    if($oldDebug == "True")
    {
      $somethingChanged = true;
      $configClass->EntryExistsOrCreate ("Debug", "False");
    }
  }
  if($enableNetwork != null && $enableNetwork != false)
  {
    $oldNetwork = $configClass->GetConfigEntryByName("EnableNetwork");
    if(!$oldNetwork || $oldNetwork != $enableNetwork)
    {
      $configClass->EntryExistsOrCreate ("EnableNetwork", $enableNetwork);
    }
    if($enableNetwork=="True")
    {
      if($network != null && $network != false)
      {
        $configClass->EntryExistsOrCreate ("NetworkPath",$network);    
      }
      if($networkUser != null && $networkUser != false)
      {
        $configClass->EntryExistsOrCreate ("NetworkUser",$networkUser);    
      }
      if($networkPas != null && $networkPas != false)
      {
        $configClass->EntryExistsOrCreate ("NetworkPassword",$networkPas);    
      }
    }
  }
  else
  {
    $oldNetwork = $configClass->GetConfigEntryByName("EnableNetwork");
    if($oldNetwork == "True")
    {
      $configClass->EntryExistsOrCreate ("EnableNetwork", "False");
    }
  }
  if($dhcpNet != null)
  {
    $somethingChanged = true;
    $configClass->AddDHCPNets($dhcpNet);
  }
  if($somethingChanged)
    $configClass->EntryExistsOrCreate ("OutOfConfigSync", 1);
}



$config = $configClass->GetAllConfig();
$connection->disconnectDB();

require '../menu/head.php';
echo '<h1>Service Konfiguration</h1>';
echo '<form name="configform" action="config.php" method="POST">';
echo '<table id="confTable"><tbody>';
echo "<tr><td>Scan Time</td><td onclick='insertTextField(this,\"scanTime\",0)'>".$config["ScanTime"].'</td></tr>';
echo "<tr><td>Timeout</td><td onclick='insertTextField(this,\"timeout\",1)'>".$config["Timeout"].'</td></tr>';
echo "<tr><td>Max Threads</td><td onclick='insertTextField(this,\"maxThreads\",2)'>".$config["MaxThreads"].'</td></tr>';
echo "<tr><td>SCP Pfad</td><td onclick='insertTextField(this,\"scp\",3)'>".str_replace("/", "\\", $config["SCPPath"]).'</td></tr>';
echo '<tr><td>Remote Start</td><td><input id="enableNetwork" type = "checkbox" name = "ennetwork" value = "True"';if($config["EnableNetwork"]== "True"){echo ' checked = "checked"';} echo ' /></td></tr>';
echo "<tr><td>Servername</td><td><input class=\"networkinput\" type=\"text\" name=\"network\" value=\"".str_replace("/", "\\", $config["NetworkPath"])."\" ";if($config["EnableNetwork"]!= "True"){echo"disabled=\"disabled\"";}echo" />".'</td></tr>';
echo "<tr><td>Username</td><td><input class=\"networkinput\" type=\"text\" name=\"networkUser\" value=\"".str_replace("/", "\\", $config["NetworkUser"])."\" ";if($config["EnableNetwork"]!= "True"){echo"disabled=\"disabled\"";}echo" />".'</td></tr>';
echo "<tr><td>Passwort</td><td><input class=\"networkinput\" type=\"password\" name=\"networkPassword\" value=\"".$config["NetworkPassword"]."\" ";if($config["EnableNetwork"]!= "True"){echo"disabled=\"disabled\"";}echo" />".'</td></tr>';
echo "<tr><td>DHCP Server Adresse</td><td onclick='insertTextField(this,\"server\",4)'>".$config["DHCPAddress"].'</td></tr>';
echo "<tr><td>Enable Debug</td><td>".'<input type = "checkbox" name = "Debug" value = "True"';if($config["Debug"]== "True"){echo ' checked = "checked"';} echo ' /></td></tr>';
echo '<tr><td>DHCP Netz Adressen</td><td><table id="dhcpNet"><tbody>';
$dhcpNet=$config["DHCPNetAddress"];
foreach($dhcpNet as $net)
{
  echo "<tr><td>$net</td><td onclick='deleteNet(this,\"$net\")'>".'<img src="../design/pictures/delete.png" width="20" height="20" alt="X"/></td></tr>';
}
echo '<tr class="line"><td><input type="text" name="dhcpnet[0]" onchange="changeDHCPNetField(this)" value="" size="15" /></td></tr>';
echo '</tbody></table>';
echo '</td></tr></tbody></table>';
echo '<p><input type="submit" value="&Uuml;bernehmen" name="submit" /></p>';
echo '</form>';
require '../menu/footer.php';